<?php
Database($_SESSION['cid']);
class NewsAction extends Action{
	
	public $tpl_path='Public/Uploads/news/news_tpl.html';
	
	//新闻列表
	public function index(){
		$rs=pageing('news',7,'',$_GET['seach'],'news_id desc','news_category=1','news_title','','DB_MEETING');
		foreach($rs['data'] as $val){
			$val['news_time']=date("Y-m-d H:i",$val['news_time']);
			$val['news_comment_count']=(int)M('news_comment','','DB_MEETING')->where('news_id='.$val['news_id'])->count();
			$arr[]=$val;
		}
		$rs['data']=$arr;
		$this->assign("rs",$rs);
		$this->display();
	}

    //添加页面
    public function to_add(){
		$_GET['category']=1;
        $rs['channel']=D('Channel')->get_all();
        $this->assign("rs",$rs);
        $this->display();
    }

	//删
	public function del_news(){
		$result = M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->find();
        $result = M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->delete();
		if ($result){
			echo '{"state":1}';
		}else{
			echo '{"state":0}';
		}
    }
	
	public function add_news(){
		$_POST['news_time']  = time();
		$text=strip_tags($_POST['news_content']);
		$text = str_replace("&","",$text);
		$text = str_replace("<","",$text);
		$text = str_replace(">","",$text);
		$text = str_replace("\n","",$text);
		$text = str_replace("\r","",$text);
		$text = str_replace("\t","",$text);
		$_POST['news_intro'] = mb_substr($text,0,30,'UTF-8');
		
		$html=file_get_contents($this->tpl_path);
		
		if($_FILES['news_image']['name']!=''){
			$this->imgUpload();
			$html=str_replace('<!--news_image-->',"<img width='100%' src='".C('DOMAIN_NAME').__ROOT__.'/'.$_POST['news_image']."' />",$html);
		}
		
		$html=str_replace('<!--news_title-->',$_POST['news_title'],$html);
		$html=str_replace('<!--news_time-->',date('Y-m-d',$_POST['news_time']),$html);
		$_POST['news_author']=$_POST['news_author']==''?'匿名':$_POST['news_author'];
		$html=str_replace('<!--news_author-->',$_POST['news_author'],$html);
		$html=str_replace('<!--news_content-->',$_POST['news_content'],$html);
		$html=str_replace('<!--news_pid-->',$_SESSION['cid'],$html);
		$news_image_path = 'Public/Uploads'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= 'news'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= $_SESSION['cid'].'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_path.= $news_image_path.time().rand(0,10000).'.html';
		
		file_put_contents($news_path,$html);
		
		$_POST['news_url'] = $news_path;
		
		$result = M('news','','DB_MEETING')->add($_POST);
		if($result){
			$this->success('添加成功',__URL__.'/index');
		}else{
			$this->error('添加失败!');	
		}
	}
	
	//改
	public function to_update(){
		$news = M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->find();
		$this->assign('news',$news);
		$rs['channel']=D('Base/Channel')->get_all();
		$this->assign("rs",$rs);
		$this->display();
	}

	public function update_news(){
		if($_FILES['news_image']['name']!=''){
			$this->imgUpload();
		}else{
			$rs = M('news','','DB_MEETING')->where('news_id='.$_POST['news_id'])->find();
			$_POST['news_image']	= $rs['news_image'];
			$_POST['news_image_s']	= $rs['news_image_s'];
		}
		$rs = M('news','','DB_MEETING')->where('news_id='.$_POST['news_id'])->find();
		$text=strip_tags($_POST['news_content']);
		$text = str_replace("&","",$text);
		$text = str_replace("<","",$text);
		$text = str_replace(">","",$text);
		$text = str_replace("\n","",$text);
		$text = str_replace("\r","",$text);
		$text = str_replace("\t","",$text);
		$_POST['news_intro'] = mb_substr($text,0,30,'UTF-8');
		$html=file_get_contents($this->tpl_path);
		$html=str_replace('<!--news_title-->',$_POST['news_title'],$html);
		if($_POST['news_image']!=''){
			$html=str_replace('<!--news_image-->',"<img width='100%' src='".C('DOMAIN_NAME').__ROOT__.'/'.$_POST['news_image']."' />",$html);
		}
		$html=str_replace('<!--news_time-->',date('Y-m-d',$rs['news_time']),$html);
		$_POST['news_author']=$_POST['news_author']==''?'匿名':$_POST['news_author'];
		$html=str_replace('<!--news_author-->',$_POST['news_author'],$html);
		$html=str_replace('<!--news_content-->',$_POST['news_content'],$html);
		$html=str_replace('<!--news_pid-->',$_SESSION['cid'],$html);
		$news_image_path = 'Public/Uploads'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= 'news'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= $_SESSION['cid'].'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_path.= $news_image_path.time().rand(0,10000).'.html';
		file_put_contents($news_path,$html);
		
		$_POST['news_url'] = $news_path;
		$rs = M('news','','DB_MEETING')->where('news_id='.$_POST['news_id'])->save($_POST);
		if($rs){
			$this->success("Update Success!");
		} else {
			$this->error("update Fail for database");
		}
	}
	
	public function imgUpload(){
		
		$news_image_path = 'Public/Uploads'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= 'news'.'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);
		$news_image_path.= $_SESSION['cid'].'/';
		if(!is_dir($news_image_path))mkdir($news_image_path,0777,true);

		import('ORG.Net.UploadFile');
		$config['savePath'] = $news_image_path;
		$config['allowExts']= array('jpg', 'gif', 'png', 'jpeg');
		$config['maxSize']= 3145728;
		$config['thumb'] = true;
		$config['thumbPrefix'] = 'm_,s_';
		$config['thumbMaxWidth'] = '480,240';
		$config['thumbMaxHeight'] = '320,160';
		$config['thumbRemoveOrigin']=true;
		$upload = new UploadFile($config);
		if(!$upload->upload()){
			$this->error($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['news_image']	=$news_image_path.'m_'.$uploadList[0]['savename'];
			$_POST['news_image_s']	=$news_image_path.'s_'.$uploadList[0]['savename'];
		}
	}

	public function news_display(){
		$rs=M('news','','DB_MEETING')->where('news_id='.$_GET['news_id'])->save(array('news_display'=>$_GET['news_display']));
		if($rs)
			echo '{"state":0}';
		else
			echo '{"state":-1}';
	}

	public function check_suggest(){
		$rs=pageing('news_comment',1000,'',$_GET['seach'],$_GET['order'],'news_id='.$_GET['news_id'],$col='content',$now='check_suggest');
		foreach($rs['data'] as $val){
			$val['time_insert']=date("Y-m-d H:i",$val['time_insert']);
			$arr[]= $val;
		}
		$rs['data']=$arr;
		$this->assign("rs",$rs);
		$this->display();
	}

	public function delete_comment(){
		$rs=M('news_comment','','DB_MEETING')->where('comment_id='.$_GET['comment_id'])->delete();
		if($rs)
			echo '{"state":0}';
		else
			echo '{"state":-1}';	
	}
//------------------------新闻频道--------------------------------------------

	//频道列表
	public function add_news_channel()
	{
		$rs=pageing('news_channel',7,'',$_GET['seach'],'channel_id desc','time_insert > time_delete','channel_name','add_news_channel','DB_MEETING');
		$this->assign("rs",$rs);
		$this->display();
	}

	public function to_add_news_channel()
	{
		$this->display();
	}

	public function addAct_news_channel()
	{
		$_POST['time_insert'] = time();
		$_POST['time_update'] = time();
		$_POST['time_delete'] = -1;
		$rs = M('news_channel','','DB_MEETING')->add($_POST);
		if($rs)
			$this->success('添加成功',__URL__.'/add_news_channel');
		else
			$this->add('添加失败');

	}

	public function del_news_channel()
	{
		$data['time_delete'] = time();
		$rs = M('news_channel','','DB_MEETING')->where('channel_id='.$_GET['channel_id'])->save($data);
		echo $rs;
	}

	public function to_update_channel()
	{
		$rs = M('news_channel','','DB_MEETING')->where('channel_id='.$_GET['channel_id'])->find();
		$this->assign('rs',$rs);
		$this->display();
	}

	public function uptAct_news_channel()
	{
		$_POST['time_update'] = time();
		$rs = M('news_channel','','DB_MEETING')->where('channel_id='.$_POST['channel_id'])->save($_POST);
		if($rs)
			$this->success('修改成功');
		else
			$this->error('修改失败');
	}

	public function deleteall(){
        $count = count($_POST['id']);
        $rs = M('news' , '' , 'DB_MEETING');
		for($i = 0 ; $i < $count ; $i++)
		{
			$sel = $rs ->where('news_id='.$_POST['id'][$i])->select();
			unlink($sel[0]['news_image']);
			unlink($sel[0]['news_image_s']);
			unlink($sel[0]['news_url']);
			$del = $rs->where('news_id='.$_POST['id'][$i])->delete();
		}
		if($del){
			$this ->success("删除成功");
		}else{
			$this ->error("删除失败"); 
		}
    }
}
?>
